package mavenWebProject;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * Servlet implementation class ProductsManageServlet
 */
@WebServlet("/ProductsManageServlet")
public class ProductsManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductsManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession sqlsession=sqlSessionFactory.openSession();
		productsMapper mapper=sqlsession.getMapper(productsMapper.class);
		String deeds = request.getParameter("do");
		String name=request.getParameter("name");
		int id=0;
		try {
		id=Integer.valueOf(request.getParameter("id"));}catch(Exception e) {}
		Date date=new Date();
		String closetime=(1900+date.getYear())+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日";
		if(deeds.contentEquals("notSell")) {
			mapper.updateProductsStatus(0,id);
			mapper.updateOrderStage(name,"退回",closetime);
			request.getRequestDispatcher("messagePage.jsp?message=商品操作").forward(request, response);
		}else if(deeds.contentEquals("onSell")) {
			mapper.updateProductsStatus(1,id);
			request.getRequestDispatcher("messagePage.jsp?message=商品操作").forward(request, response);
		}else if(deeds.contentEquals("remove")) {
			mapper.deleteProducts(id);
			mapper.updateOrderStage(name,"退回",closetime);
			request.getRequestDispatcher("messagePage.jsp?message=商品操作").forward(request, response);
		}else if(deeds.contentEquals("editProducts")){
			String changename=request.getParameter("name");
			String changeprice=request.getParameter("price");
			String changestorage=request.getParameter("storage");
			String changeall_ordered=request.getParameter("all_ordered");
			String changefor_group=request.getParameter("for_group");
			String changekind=request.getParameter("kind");
			mapper.updateProducts(id, changename, Integer.valueOf(changeprice), Integer.valueOf(changestorage), Integer.valueOf(changeall_ordered), changefor_group, changekind);
			request.getRequestDispatcher("messagePage.jsp?message=商品操作").forward(request, response);
		}else if(deeds.contentEquals("addProducts")){
			String addname=request.getParameter("name");
			String addimg=request.getParameter("img");
			int addprice=Integer.valueOf(request.getParameter("price"));
			int addstorage=Integer.valueOf(request.getParameter("storage"));
			int addsold=0;int addstatus=1;
			int addall_ordered=Integer.valueOf(request.getParameter("all_ordered"));
			String addfor_group=request.getParameter("for_group");
			String addkind=request.getParameter("kind");
			mapper.insertProducts(addname, addimg, addprice, addstorage, addsold, addstatus, addall_ordered, addfor_group, addkind);
			request.getRequestDispatcher("messagePage.jsp?message=商品操作").forward(request, response);
		}
		sqlsession.commit();
		sqlsession.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
